<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    kk_balance:'',
    title:'',
    typeOptions:[] as Array<any>
  })
  interface formQuert {
    id:number
    name:string
    icon:string
    start_time:string
    end_time:string
    type:number
    exec_url:string
    days:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    name: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
    icon: [{ required: true, message: "请上传icon图标", trigger: "blur" }],
    start_time: [{ required: true, message: "请选择活动开始时间", trigger: "blur" }],
    end_time: [{ required: true, message: "请选择活动结束时间", trigger: "blur" }],
    type: [{ required: true, message: "请选择名单类型", trigger: "blur" }],
    exec_url: [{ required: true, message: "请上传用户名单", trigger: "blur" }],
    days: [{ required: true, message: "请输入个人持续天数", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { id } = form
      if(id) {
        await gamesApi.edit_act_mission(form).then(()=>{
          ToastSuccess('编辑成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      else {
        await gamesApi.add_act_mission_config(form).then(()=>{
          ToastSuccess('新增成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any={})=> {
    const {
      typeOptions=[],
      id, ac_name:name, icon='', start_time, end_time,
      type, execl_url:exec_url='', days,
    } = query
    action.typeOptions = typeOptions
    Object.assign(form,{
      id, name, icon, start_time, end_time,
      type, exec_url, days,
    })
    action.dialogFormVisible = true
    action.title = id?`编辑活动：${name}`:'创建活动'
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    action.kk_balance = ''
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="52%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="活动名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="开始时间" prop="start_time">
          <el-date-picker
            v-model="form.start_time"
            type="date"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            placeholder="请选择活动开始时间" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="结束时间" prop="end_time">
          <el-date-picker
            v-model="form.end_time"
            type="date"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            placeholder="请选择活动结束时间" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="个人持续天数" prop="days">
          <el-input v-model="form.days" @input="allNumber(form,'days')" placeholder="请输入个人持续天数" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="名单类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择名单类型">
            <el-option v-for="item in action.typeOptions" :label="item.text" :value="item.type" />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="用户名单" prop="exec_url">
          <xmUpload listType="text" v-model:file-url="form.exec_url" :data="{type:24}" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="任务图标" prop="icon">
          <xmUpload v-model:file-url="form.icon" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>